#include "exporttable.h"
#include "global.h"

#include <QDir>
#include <QMessageBox>

ExportTable::ExportTable(QWidget *parent) : QWidget(parent)
{
    setParent(parent);

    pXlsx = new QXlsx::Document;
    pHeadFormat = new QXlsx::Format;
    pItemDayFormat = new QXlsx::Format;
    pItemNightFormat = new QXlsx::Format;
    pItemCommonFormat = new QXlsx::Format;
    pSpecialHeadFormat = new QXlsx::Format;
    pItemSpeciaFormat = new QXlsx::Format;
    
}

void ExportTable::startExportTable(QList<MemberClass *> memberList, QList<myDay *> dayList)
{
    createTableSheet();
    setXlsxHeadFormat();
    setXlsxItemFormat();

    writeHead(memberList, dayList);
    writeItem(memberList, dayList);

    exportXlsx();
    return;
}

void ExportTable::createTableSheet()
{
    if(NULL == pXlsx){
        QLOG_WARN() << "NULL == pXlsx";
        return;
    }

    QString name = QString("%1年%2月").arg(CURRENT_YEAR).arg(CURRENT_MONTH);
    pXlsx->addSheet(name);
    return;
}

void ExportTable::setXlsxHeadFormat()
{
    if(NULL == pHeadFormat){
        QLOG_WARN() << "NULL == pHeadFormat";
        return;
    }

    pHeadFormat->setFont(QFont("黑体"));
    pHeadFormat->setFontColor(QColor(Qt::black));         // 文字颜色
    pHeadFormat->setPatternBackgroundColor(QColor(224,224,224));     // 背景颜色
    pHeadFormat->setFontSize(16);/*设置字体大小*/
    pHeadFormat->setHorizontalAlignment(QXlsx::Format::AlignHCenter);
    pHeadFormat->setVerticalAlignment(QXlsx::Format::AlignVCenter);
    pHeadFormat->setBorderColor(QColor(Qt::black));
    pHeadFormat->setBorderStyle(QXlsx::Format::BorderThin);

    *pSpecialHeadFormat = *pHeadFormat;
    pSpecialHeadFormat->setFontSize(15);
    pSpecialHeadFormat->setPatternBackgroundColor(SCHEDULE_TABLE_SPECIFY_ITEM_COLOR);

    return;
}

void ExportTable::setXlsxItemFormat()
{
    if(NULL == pItemDayFormat || NULL == pItemNightFormat ||  NULL == pItemCommonFormat){
        QLOG_WARN() << "NULL == pItemDayFormat || NULL == pItemNightFormat ||  NULL == pItemCommonFormat";
        return;
    }

    pItemDayFormat->setFont(QFont("宋体"));
    pItemDayFormat->setFontColor(QColor(Qt::black));         // 文字颜色
    pItemDayFormat->setPatternBackgroundColor(SCHEDULE_TABLE_DAY_ITEM_COLOR);     // 背景颜色
    pItemDayFormat->setFontSize(14);/*设置字体大小*/
    pItemDayFormat->setHorizontalAlignment(QXlsx::Format::AlignHCenter);
    pItemDayFormat->setVerticalAlignment(QXlsx::Format::AlignVCenter);
    pItemDayFormat->setBorderColor(QColor(Qt::black));
    pItemDayFormat->setBorderStyle(QXlsx::Format::BorderThin);

    // 夜班除了颜色不一样，其他都一样
    *pItemNightFormat = *pItemDayFormat;
    pItemNightFormat->setPatternBackgroundColor(SCHEDULE_TABLE_NIGHT_ITEM_COLOR);

    *pItemCommonFormat = *pItemDayFormat;
    pItemCommonFormat->setPatternBackgroundColor(QColor(Qt::white));

    *pItemSpeciaFormat = *pItemDayFormat;
    pItemSpeciaFormat->setPatternBackgroundColor(SCHEDULE_TABLE_SPECIFY_ITEM_COLOR);

    return;
}

void ExportTable::writeHead(QList<MemberClass *> &memberList, QList<myDay *> &dayList)
{
    if(NULL == pXlsx || NULL == pHeadFormat){
        QLOG_WARN() << "NULL == pXlsx || NULL == pHeadFormat";
        return;
    }

    QString daystr;
    QStringList collist = {"白班人数", "夜班人数", "总人数", "备注"};
    QStringList rowlist = {"个人白班小计", "个人夜班小计", "个人班时总计"};
    myDay* pDay = NULL;
    MemberClass * pMember = NULL;
    pXlsx->setColumnWidth(1, 1, 19);
    pXlsx->setColumnWidth(2, memberList.count() + 1, 15);
    pXlsx->setColumnWidth(memberList.count() + 2, memberList.count() + 2 +collist.count() , 16);
    pXlsx->setRowHeight(1, dayList.count() + 1, 21);


    for(int i = 0; i < memberList.count(); i++){
        pMember = memberList.at(i);
        if(NULL == pMember)            continue;

        pXlsx->write(1, i + 2, pMember->getMemberName(), *pHeadFormat);
    }

    for(int i = 0; i < dayList.count(); i++){
        pDay = dayList.at(i);
        if(NULL == pDay)            continue;

        daystr = QString("%1月%2日").arg(pDay->getMonth()).arg(pDay->GetDayOfMonth() + 1);
        pXlsx->write(i + 2, 1, daystr, *pHeadFormat);
    }

    for(int i = 0; i < collist.count(); i++)
        pXlsx->write(1, memberList.count() + i + 2, collist.at(i), *pSpecialHeadFormat);

    for(int i = 0; i < rowlist.count(); i++)
        pXlsx->write(dayList.count() + i + 2, 1, rowlist.at(i), *pSpecialHeadFormat);

    return;
}

void ExportTable::writeItem(QList<MemberClass *> &memberList, QList<myDay *> &dayList)
{
    if(NULL == pXlsx || NULL == pItemDayFormat || NULL == pItemNightFormat || NULL == pItemCommonFormat ){
        QLOG_WARN() << "NULL == pXlsx || NULL == pItemDayFormat || NULL == pItemNightFormat || NULL == pItemCommonFormat ";
        return;
    }

    myDay* pDay = NULL;
    MemberClass * pMember = NULL;
    for(int i = 0; i < memberList.count(); i++){
        pMember = memberList.at(i);
        if(NULL == pMember)            continue;

        for(int j = 0; j < dayList.count(); j++){
            pDay = dayList.at(j);
            if(NULL == pDay)            continue;

            if(true == pDay->getMemberList(DATESET_DAYWORK).contains(pMember->getMemberName()))
                pXlsx->write(j + 2, i + 2, "白", *pItemDayFormat);
            else if(true == pDay->getMemberList(DATESET_NIGHTWORK).contains(pMember->getMemberName()))
                pXlsx->write(j + 2, i + 2, "夜", *pItemNightFormat);
            else
                pXlsx->write(j + 2, i + 2, "", *pItemCommonFormat);
        }
    }

    writeSpecialItem(memberList, dayList);

    return;
}

void ExportTable::writeSpecialItem(QList<MemberClass *> &memberList, QList<myDay *> &dayList)
{
    if(NULL == pXlsx || NULL == pItemSpeciaFormat){
        QLOG_WARN() << "NULL == pXlsx || NULL == pItemSpeciaFormat ";
        return;
    }

    QString formulstr1 = QString("=COUNTIF(%1%2:%3%4,\"%5\")");
    QString formulstr2 = QString("=SUM(%1%2:%3%4)");
    QString formulstr3 = QString("=%1%2+(%3%4)*2");
    char ch1 = 'A' + memberList.count();
    char ch2 = ch1 + 1;
    QString tmp1;
    QString tmp2;
    QString tmp3;

    for(int i = 0; i < dayList.count(); i++){
        tmp1 = formulstr1.arg("B").arg(i + 2).arg(ch1).arg(i + 2).arg("白");
        tmp2 = formulstr1.arg("B").arg(i + 2).arg(ch1).arg(i + 2).arg("夜");
        tmp3 = formulstr2.arg(ch2).arg(i + 2).arg(char(ch2 + 1)).arg(i + 2);
        pXlsx->write(i + 2, memberList.count() + 1 + 1, tmp1, *pItemSpeciaFormat);
        pXlsx->write(i + 2, memberList.count() + 1 + 2, tmp2, *pItemSpeciaFormat);
        pXlsx->write(i + 2, memberList.count() + 1 + 3, tmp3, *pItemSpeciaFormat);
    }

    for(int i = 0; i < memberList.count(); i++){
        tmp1 = formulstr1.arg(char('B' + i)).arg(2).arg(char('B' + i)).arg(dayList.count() + 1).arg("白");
        tmp2 = formulstr1.arg(char('B' + i)).arg(2).arg(char('B' + i)).arg(dayList.count() + 1).arg("夜");
        tmp3 = formulstr3.arg(char('B' + i)).arg(dayList.count() + 2).arg(char('B' + i)).arg(dayList.count() + 2 + 1);
        pXlsx->write(dayList.count() + 1 + 1, i + 2, tmp1, *pItemSpeciaFormat);
        pXlsx->write(dayList.count() + 1 + 2, i + 2, tmp2, *pItemSpeciaFormat);
        pXlsx->write(dayList.count() + 1 + 3, i + 2, tmp3, *pItemSpeciaFormat);
    }

    return;
}

void ExportTable::exportXlsx()
{
    if(NULL == pXlsx){
        QLOG_WARN() << "NULL == pXlsx";
        return;
    }

    QString name = QString("%1年%2月排班.xlsx").arg(CURRENT_YEAR).arg(CURRENT_MONTH);
    QString filepath = QDir::currentPath() +"/" + name;

    QMessageBox *pBox = NULL;
    // 导出成功
    if(true == pXlsx->saveAs(name)){
        pBox = new QMessageBox(QMessageBox::Information, "成功", QString("文件导出成功，文件路径:%1").arg(filepath), QMessageBox::Ok, this, Qt::Dialog);
        QLOG_INFO() << "文件导出成功，" << filepath;
        pBox->exec();
    }
    // 导出失败
    else{
        pBox = new QMessageBox(QMessageBox::Critical, "失败", QString("文件导出失败，请查看当前路径是否已打开同名文件，文件路径:%1").arg(filepath), QMessageBox::Ok, this, Qt::Dialog);
        QLOG_INFO() << "文件导出失败，" << filepath;
        pBox->exec();
    }

    return;
}

void ExportTable::slot_exportTable_triggered(QList<MemberClass *> memberList, QList<myDay *> dayList)
{
    startExportTable(memberList, dayList);

    return;
}
